package com.example.demo.util;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

/**
 * 
 * @title:	RSAUtil
 * @author:	许子文
 * @date:	2021年3月30日 
 * @descirption: RSA工具类
 *
 */
public class RSAUtil {
	private static Map<Integer, String> keyMap = new HashMap<>();
	
	/**
	 * 
	 * @title genKeyPair
	 * @author: 许子文
	 * @date: 2021年3月30日
	 * @description: 生成密钥对
	 */
	public static String genKeyPair() throws Exception {
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
		
		keyPairGen.initialize(1024, new SecureRandom());
		
		KeyPair keyPair = keyPairGen.generateKeyPair();
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		
		String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
		String privateKeyString = new String(Base64.encodeBase64(privateKey.getEncoded()));
		
		keyMap.put(0, publicKeyString);
		keyMap.put(1, privateKeyString);
		
		return publicKeyString + ":" + privateKeyString;
	}
	
	/**
	 * 
	 * @方法名 RSAUtil.java
	 * @创建者 xzw
	 * @创建日期 2021年3月30日
	 * @返回类型 String
	 * @功能描述 RSA公钥加密
	 * @param str 明文
	 * @param publicKey 公钥
	 */
	public static String encrypt(String str,String publicKey) throws Exception {
		byte[] decoded = Base64.decodeBase64(publicKey);
		
		RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
		
		Cipher cipher = Cipher.getInstance("RSA");
		
		cipher.init(Cipher.ENCRYPT_MODE,pubKey);
		
		String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
		
		return outStr;
	}
	
	/**
	 * 
	 * @方法名 RSAUtil.java
	 * @创建者 xzw
	 * @创建日期 2021年3月30日
	 * @返回类型 String
	 * @功能描述 RSA私钥解密
	 * @param str加密字符串
	 * @param 私钥
	 * 
	 */
	public static String decrypt(String str,String privateKey) throws Exception {
		byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
		
		byte[] decoded = Base64.decodeBase64(privateKey);
		
		RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
		
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, priKey);
		String outStr = new String(cipher.doFinal(inputByte));
		
		return outStr;
	}
	
	public static void main(String[] args) throws Exception{
		genKeyPair();
		
		String message = "xiaoshi950704";
		
		String pbKey = keyMap.get(0);
		String piKey = keyMap.get(1);
		
		System.out.println("随机生成的公钥为>>>"+pbKey);
		System.out.println("随机生成的私钥为>>>"+piKey);
		
		String enMessage = encrypt(message, pbKey);
		
		System.out.println("加密后的字符串为>>>"+enMessage);
		
		System.out.println("修改过后的私钥=="+piKey);
		
		String deMessage = decrypt(enMessage, piKey);
		
		System.out.println("解密后的字符串为>>>"+deMessage);
	}
}
